package com.dome.backtemplate.config.filter;

import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.dome.backtemplate.constant.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TODO
 * @Author 小乌龟
 * @Date 2023/4/8 10:01
 */
@Component
public class UserInterceptor implements HandlerInterceptor{

//    @Autowired
//    private ObUserService obUserService;
//

    private static UserInterceptor userInterceptor;

    @PostConstruct
    public void init() {
        userInterceptor = this;
//        userInterceptor.obUserService = this.obUserService;

    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //http的header中获得token
        String token = request.getHeader(SystemConstant.TOKEN_HEAD);
        //token不存在
        if (StrUtil.isNotBlank(token)) {
//            if (JWTUtil.verify(token,SystemConstant.JWT_KEY.getBytes())){
//                JWT jwt = JWTUtil.parseToken(token);
//                String old_box_uid = (String) jwt.getPayload(SystemConstant.JWT_USER_INFO);
//                //验证用户是否存在
//                if(userInterceptor.obUserService.isUser(old_box_uid)){          
                    return true;
//                }

        }
      //   如果权限启用 开启false 拒绝无权限访问
      //   return false;
        return true;
    }
}